<?php

namespace BeyondIM\RTS;

class HideContentInPosts extends FormatContent
{
  public function __invoke($serializer, $model, $attributes)
  {
    if (isset($attributes["contentHtml"])) {
      $discussion = $model->discussion;
      $actor = $serializer->getActor();

      // 如隐藏内容不在 1 楼时, 直接显示
      if ($discussion->first_post_id != $model->id) {
        $attributes["contentHtml"] = preg_replace('/<reply>(.*?)<\/reply>/is', '<div>$1</div>', $attributes["contentHtml"]);
        $attributes["contentHtml"] = preg_replace('/<lv req=".*?">(.*?)<\/lv>/is', '<div>$1</div>', $attributes["contentHtml"]);
        return $attributes;
      }

      // 发帖人是自己时, 显示隐藏内容(不需回复)
      if ($actor->id == $model->user_id) {
        $attributes["contentHtml"] = preg_replace('/<reply>(.*?)<\/reply>/is', '<div class="rts"><div class="rts_title">' . $this->translator->trans('beyondim-rts.forum.hidden_content') . '</div>$1</div>', $attributes["contentHtml"]);
        $attributes["contentHtml"] = preg_replace('/<lv req=.*?>(.*?)<\/lv>/is', '<div class="rts"><div class="rts_title">' . $this->translator->trans('beyondim-rts.forum.hidden_content') . '</div>$1</div>', $attributes["contentHtml"]);
        return $attributes;
      }

      if ($this->requires('reply')) {
        // 如有权限略过回复可见(比如管理员, 高级会员), 直接显示隐藏内容
        if ($actor->hasPermission('beyondim-rts.can_bypass_reply')) {
          $attributes["contentHtml"] = preg_replace('/<reply>(.*?)<\/reply>/is', '<div class="rts"><div class="rts_title">' . $this->translator->trans('beyondim-rts.forum.hidden_content') . '</div>$1</div>', $attributes["contentHtml"]);
          $attributes["contentHtml"] = preg_replace('/<lv req=.*?>(.*?)<\/lv>/is', '<div class="rts"><div class="rts_title">' . $this->translator->trans('beyondim-rts.forum.hidden_content') . '</div>$1</div>', $attributes["contentHtml"]);
          return $attributes;
        } else {
          $usersModel = $discussion->participants()->get('id');
          $users = [];
          foreach ($usersModel as $user) {
            $users[] = $user->id;
          }
          $replied = !$actor->isGuest() && in_array($actor->id, $users);

          if ($replied) {
            if (strpos($attributes['contentHtml'], '<reply') !== false) {
              $attributes["contentHtml"] = preg_replace('/<reply>(.*?)<\/reply>/is', '<div class="rts"><div class="rts_title">' . $this->translator->trans('beyondim-rts.forum.hidden_content') . '</div>$1</div>', $attributes["contentHtml"]);
            }
            if (strpos($attributes['contentHtml'], '<lv req=') !== false) {
              $expTotal = $actor->discussion_count * 2 + ($actor->comment_count - $actor->discussion_count) * 0.5 + ($actor->clarkwinkelmann_likes_received_count) * 3;
              $expLevel = floor(sqrt(($expTotal / 50) * 2 + 0.25) + 0.5);
              preg_match('/<lv req="(.*?)">.*?<\/lv>/is', $attributes['contentHtml'], $match);
              if ($match[1] > $expLevel) {
                $attributes["contentHtml"] = preg_replace('/<lv req=.*?>.*?<\/lv>/is', '<div class="rts"><div class="rts_alert">' . $this->translator->trans('beyondim-rts.forum.lv', array('{level}' => 'Lv.' . $match[1])) . '</div></div>', $attributes["contentHtml"]);
              } else {
                $attributes["contentHtml"] = preg_replace('/<lv req=.*?>(.*?)<\/lv>/is', '<div class="rts"><div class="rts_title">' . $this->translator->trans('beyondim-rts.forum.hidden_content') . '</div>$1</div>', $attributes["contentHtml"]);
              }
            }
          } else {
            if (strpos($attributes['contentHtml'], '<reply') !== false) {
              $attributes["contentHtml"] = preg_replace('/<reply>.*?<\/reply>/is', '<div class="rts"><div class="rts_alert">' . $this->translator->trans('beyondim-rts.forum.reply') . '</div></div>', $attributes["contentHtml"]);
            }
            if (strpos($attributes['contentHtml'], '<lv req=') !== false) {
              preg_match('/<lv req="(.*?)">.*?<\/lv>/is', $attributes['contentHtml'], $match);
              $attributes["contentHtml"] = preg_replace('/<lv req=.*?>.*?<\/lv>/is', '<div class="rts"><div class="rts_alert">' . $this->translator->trans('beyondim-rts.forum.lv', array('{level}' => 'Lv.' . $match[1])) . '</div></div>', $attributes["contentHtml"]);
            }
          }
        }
      }
    }
    return $attributes;
  }

  private function requires(string $key)
  {
    return $this->settings->get('beyondim-rts.admin.settings.require_'.$key, false);
  }

}
